/*
 * @Author: xiaosihan 
 * @Date: 2022-07-16 17:28:31 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-08-11 20:41:35
 */

import { Vector3 } from "three";
import ThreeBase from "three-base";
import { degToRad } from "three/src/math/MathUtils";
import model3dRender from "../model3dRender";
import TextPlanMesh from "./TextPlanMesh";


class CubeController extends ThreeBase {

    constructor() {
        super();
    }

    model3dRenderCameraLength = 0;

    // 立方体控制器操作中
    playing = false;

    init() {
        this.controls.panSpeed = 0;
        this.controls.maxDistance = 4;
        this.controls.minDistance = 4;
        this.controls.enableDamping = false;

        const { center, position } = model3dRender.getLookAt();

        this.setLookAt({
            center: new Vector3(0, 0, 0),
            position: new Vector3(position.x - center.x, position.y - center.y, position.z - center.z)
        });

        this.controls.addEventListener("change", (e) => {
            if (this.playing) {
                model3dRender.getCamera().getWorldPosition(this.cameraPosition);
                this.cameraPosition.sub(model3dRender.controls.target);
                this.model3dRenderCameraLength = this.cameraPosition.length();
                this.getCamera().getWorldPosition(this.cameraPosition);
                this.cameraPosition.setLength(this.model3dRenderCameraLength);
                this.cameraPosition.add(model3dRender.controls.target);
                model3dRender.getCamera().position.copy(this.cameraPosition);
                model3dRender.getCamera().lookAt(model3dRender.controls.target);
            }
        });

        this.renderer.domElement.addEventListener("mouseenter", () => {
            this.playing = true;
        });

        this.renderer.domElement.addEventListener("mouseleave", () => {
            this.playing = false;
        });


        super.init();
    }


    left = (() => {
        const left = new TextPlanMesh("左");
        left.rotateY(degToRad(-90));
        left.position.set(-0.5, 0, 0);

        left.userData = { cursor: "pointer", enableEvent: true };
        left.addEventListener("leftclick", e => {

            const distance = model3dRender.controls.getDistance();
            const center = model3dRender.controls.target;
            model3dRender.setLookAt({
                duration: 500,
                center,
                position: new Vector3(center.x - distance, center.y, center.z)
            });
        });

        left.addEventListener("mouseenter", e => {
            left.setHeightLight(true);
        });

        left.addEventListener("mouseleave", e => {
            left.setHeightLight(false);
        });

        this.scene.add(left);
        return left;
    })();

    right = (() => {
        const right = new TextPlanMesh("右");
        right.rotateY(degToRad(90));
        right.position.set(0.5, 0, 0);

        right.userData = { cursor: "pointer", enableEvent: true };
        right.addEventListener("leftclick", e => {
            const distance = model3dRender.controls.getDistance();
            const center = model3dRender.controls.target;
            model3dRender.setLookAt({
                duration: 500,
                center,
                position: new Vector3(center.x + distance, center.y, center.z)
            });
        });

        right.addEventListener("mouseenter", e => {
            right.setHeightLight(true);
        });

        right.addEventListener("mouseleave", e => {
            right.setHeightLight(false);
        });

        this.scene.add(right);
        return right;
    })();

    top = (() => {
        const top = new TextPlanMesh("上");
        top.rotateX(degToRad(-90));
        top.position.set(0, 0.5, 0);

        top.userData = { cursor: "pointer", enableEvent: true };
        top.addEventListener("leftclick", e => {
            const distance = model3dRender.controls.getDistance();
            const center = model3dRender.controls.target;
            const direction = new Vector3()
                .copy(model3dRender.getCamera().position)
                .sub(center)
                .setY(0)
                .setLength(100);

            model3dRender.setLookAt({
                duration: 500,
                center,
                position: new Vector3(center.x, center.y + distance, center.z + 1)
            });
        });

        this.scene.add(top);
        return top;
    })();

    bottom = (() => {
        const bottom = new TextPlanMesh("下");
        bottom.rotateX(degToRad(90));
        bottom.position.set(0, -0.5, 0);

        bottom.userData = { cursor: "pointer", enableEvent: true };
        bottom.addEventListener("leftclick", e => {
            const distance = model3dRender.controls.getDistance();
            const center = model3dRender.controls.target;
            const direction = new Vector3()
                .copy(model3dRender.getCamera().position)
                .sub(center)
                .setY(0)
                .setLength(100);

            model3dRender.setLookAt({
                duration: 500,
                center,
                position: new Vector3(center.x, center.y - distance, center.z + 1)
            });
        });

        this.scene.add(bottom);
        return bottom;
    })();

    front = (() => {
        const front = new TextPlanMesh("前");
        front.rotateX(degToRad(0));
        front.position.set(0, 0, 0.5);

        front.userData = { cursor: "pointer", enableEvent: true };
        front.addEventListener("leftclick", e => {
            const distance = model3dRender.controls.getDistance();
            const center = model3dRender.controls.target;
            model3dRender.setLookAt({
                duration: 500,
                center,
                position: new Vector3(center.x, center.y, center.z + distance)
            });
        });

        front.addEventListener("mouseenter", e => {
            front.setHeightLight(true);
        });

        front.addEventListener("mouseleave", e => {
            front.setHeightLight(false);
        });

        this.scene.add(front);
        return front;
    })();

    back = (() => {
        const back = new TextPlanMesh("后");
        back.rotateY(degToRad(180));
        back.position.set(0, 0, -0.5);

        back.userData = { cursor: "pointer", enableEvent: true };
        back.addEventListener("leftclick", e => {
            const distance = model3dRender.controls.getDistance();
            const center = model3dRender.controls.target;
            model3dRender.setLookAt({
                duration: 500,
                center,
                position: new Vector3(center.x, center.y, center.z - distance)
            });
        });

        back.addEventListener("mouseenter", e => {
            back.setHeightLight(true);
        });

        back.addEventListener("mouseleave", e => {
            back.setHeightLight(false);
        });

        this.scene.add(back);
        return back;
    })();

    cameraPosition = new Vector3();

    render() {

        // if (!this.playing) {
        model3dRender.getCamera().getWorldPosition(this.cameraPosition);
        this.cameraPosition.sub(model3dRender.controls.target);
        this.getCamera().position.copy(this.cameraPosition);
        // }

        super.render();
    }


}

const cubeController = new CubeController();
export default cubeController;